package com.winfield.photovideo;

import java.io.File;

import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MakeVideoActivity extends Activity {

	private SurfaceView surfaceView;
	private RelativeLayout layout;
	private Button recordbutton;
	private Button stopbutton;
	private String fileName;   //上传的资源的文件名
	private String filePath;	//上传资源所存放的路径
	private String userID;		//上传资源所拥有用户的ID
	private MediaRecorder mediaRecorder;//刻录要用到媒体刻录这个类
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        		WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.makevideoactivity);
        
        
        //intent是用于激活它的对象
		Intent intent = getIntent();
        //获取userID
        Bundle bundle = intent.getExtras();
		userID = bundle.getString("userID");
   
        surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);
        //surfaceView参数设置
        //将摄像头给他的东西  直接推送到surface前方  显示出来
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        //图像分辨率
        surfaceView.getHolder().setFixedSize(176, 144);
        //要求屏幕常亮 不要锁屏
        surfaceView.getHolder().setKeepScreenOn(true);
        layout = (RelativeLayout)this.findViewById(R.id.makevideolayout);
        recordbutton = (Button)this.findViewById(R.id.recordbutton);
        stopbutton = (Button)this.findViewById(R.id.stopbutton);
    }

    
    /**
     * 重新触摸屏幕的方法
     */

    @Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_DOWN){
			layout.setVisibility(ViewGroup.VISIBLE);
		}
		return super.onTouchEvent(event);
	}


    public void record(View v){
    	
    	switch(v.getId()){
    	case R.id.recordbutton:
    		try{
	    		//点击刻录按钮事件
	    		mediaRecorder = new MediaRecorder();
	    		//设置声音来源  ：麦克风
	    		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
	    		//设置画面来源：摄像头
	    		mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
	    		//设置输出格式：3gp
	    		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
	    		//设置视频大小：820*240
	    		mediaRecorder.setVideoSize(320, 240);
	    		//设置每秒中捕获的画面：5
	    		mediaRecorder.setVideoFrameRate(5);
	    		//设置声音的编码 ：amr
	    		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
	    		//设置视频的编码：H264 比263高级
	    		mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
	    		
	    		//设置文件的输出位置：sd卡的根目录
	    		//File  构造的两个参数  第一个是  存放的路径名  第二个是文件名称
				fileName = System.currentTimeMillis()+".3gp";
				
				filePath =Environment.getExternalStorageDirectory().getAbsolutePath();
	    		
	    		
	    		File videoFile = new File(filePath,fileName);
	    		mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
	    		//设置预览
	    		
	    		
	    		//刻录
	    		mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
	    		//进行缓冲
	    		mediaRecorder.prepare();
	    		//开始刻录
	    		mediaRecorder.start();
	    		
	    		
	    		
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    		recordbutton.setEnabled(false);
    		stopbutton.setEnabled(true);
    		
    		break; 
    	case R.id.stopbutton:
    		//点击的是停止按钮
    		if(mediaRecorder!=null){
    			mediaRecorder.stop();
    			mediaRecorder.release();
    			mediaRecorder = null;
    		}
    		recordbutton.setEnabled(true);
    		stopbutton.setEnabled(false);
    		
    		//TODO
    		//将文件上传
			Toast.makeText(getApplicationContext(), R.string.makevideosuccess, 1).show();
			openUploadActivity(filePath,fileName);
    		break;  
    	default:
    		break;
    		
    	}
    }

	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
	/**
	 * 改方法用于打开上传界面
	 * @param filePath  拍摄资源所在路径
	 * @param fileName  拍摄资源的名称
	 */
	public void openUploadActivity(String filePath,String fileName){
		//构建意图对象
    	Intent intent = new Intent();
    	//设置要激活的组件  上下文+要激活的组件的名称  方法1
    	intent.setClass(this, UploadActivity.class);
    	//利用bundle对象  进行参数设置
    	Bundle bundle = new Bundle();
    	bundle.putString("userID", userID);
    	bundle.putString("filePath", filePath);
    	bundle.putString("fileName", fileName);
    	intent.putExtras(bundle);
    	
    	//启动意图  将自动激活里面的参数
    	startActivity(intent);
	}
}
